/************************************************************************************/
/*  Program name CAPAPEXT_FIN_OPT.sql                                               */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
set verify off;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAP_FIN_OPT';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPAP_FIN_OPT.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_SOB_SName	VARCHAR2(100);
	lv_SOB_Count	NUMBER;

	CURSOR Cur_F(p_NGO_SOB VARCHAR2) IS
	SELECT
		F.*
	FROM
		GL_SETS_OF_BOOKS				B,
		FINANCIALS_SYSTEM_PARAMS_ALL	F
	WHERE	UPPER(B.Short_Name)	=	p_NGO_SOB
	AND		B.Set_Of_Books_ID	=	F.Set_Of_Books_ID;

BEGIN
	lv_SOB_SName	:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_SOB_Count
	FROM	GL_SETS_OF_BOOKS
	WHERE	UPPER(Short_Name)	=	lv_SOB_SName;

	IF	lv_SOB_Count	=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_SOB_SName) LOOP

			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Last_Update_Date||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
					C1.Set_Of_Books_ID||CHR(9)||
				C1.Payment_Method_Lookup_Code||CHR(9)||
				C1.User_Defined_Vendor_Num_Code||CHR(9)||
				C1.Vendor_Num_Start_Num||CHR(9)||
					C1.Ship_To_Location_ID||CHR(9)||
					C1.Bill_To_Location_ID||CHR(9)||
				C1.Ship_Via_Lookup_Code||CHR(9)||
				C1.Fob_Lookup_Code||CHR(9)||
					C1.Terms_ID||CHR(9)||
				C1.Always_Take_Disc_Flag||CHR(9)||
				C1.Pay_Date_Basis_Lookup_Code||CHR(9)||
				C1.Invoice_Currency_Code||CHR(9)||
				C1.Payment_Currency_Code||CHR(9)||
					C1.Accts_Pay_Code_Combination_ID||CHR(9)||
					C1.Prepay_Code_Combination_ID||CHR(9)||
					C1.Disc_Taken_Code_Combination_ID||CHR(9)||
				C1.Future_Period_Limit||CHR(9)||
				C1.Reserve_At_Completion_Flag||CHR(9)||
					C1.Res_Encumb_Code_Combination_ID||CHR(9)||
				C1.Req_Encumbrance_Flag||CHR(9)||
				C1.Req_Encumbrance_Type_ID||CHR(9)||		--  ???
				C1.Purch_Encumbrance_Flag||CHR(9)||
				C1.Purch_Encumbrance_Type_ID||CHR(9)||
				C1.Inv_Encumbrance_Type_ID||CHR(9)||		--  ???
				C1.Manual_Vendor_Num_Type||CHR(9)||
					C1.Inventory_Organization_ID||CHR(9)||
				C1.Last_Update_Login||CHR(9)||
				C1.Creation_Date||CHR(9)||
					C1.Created_By||CHR(9)||
				C1.Freight_Terms_Lookup_Code||CHR(9)||
				C1.Rfq_Only_Site_Flag||CHR(9)||
				C1.Receipt_Acceptance_Days||CHR(9)||
					C1.Business_Group_ID||CHR(9)||
				C1.Expense_Check_Address_Flag||CHR(9)||
				C1.Terms_Date_Basis||CHR(9)||
				C1.Use_Positions_Flag||CHR(9)||
					C1.Rate_Var_Code_Combination_ID||CHR(9)||
				C1.Hold_Unmatched_Invoices_Flag||CHR(9)||
				C1.Exclusive_Payment_Flag||CHR(9)||
				C1.Revision_Sort_Ordering||CHR(9)||
				C1.Vat_Registration_Num||CHR(9)||
				C1.Vat_Country_Code||CHR(9)||
					C1.Rate_Var_Gain_CCID||CHR(9)||
					C1.Rate_Var_Loss_CCID||CHR(9)||
					C1.Org_ID||CHR(9)||
				C1.Bank_Charge_Bearer||CHR(9)||
				C1.Vat_Code||CHR(9)||
				C1.Match_Option||CHR(9)||
				C1.Non_Recoverable_Tax_Flag||CHR(9)||
				C1.Tax_Rounding_Rule||CHR(9)||
				C1.Precision||CHR(9)||
				C1.Minimum_Accountable_Unit||CHR(9)||
				C1.Default_Recovery_Rate||CHR(9)||
				C1.Cash_Basis_Enc_Nr_Tax||CHR(9)||
					C1.Future_Dated_Payment_CCID||CHR(9)||
					C1.Expense_Clearing_CCID||CHR(9)||
					C1.Misc_Charge_CCID||CHR(9)||
				C1.Global_Attribute_Category||CHR(9)||
				C1.Global_Attribute1||CHR(9)||
				C1.Global_Attribute2||CHR(9)||
				C1.Global_Attribute3||CHR(9)||
				C1.Global_Attribute4||CHR(9)||
				C1.Global_Attribute5||CHR(9)||
				C1.Global_Attribute6||CHR(9)||
				C1.Global_Attribute7||CHR(9)||
				C1.Global_Attribute8||CHR(9)||
				C1.Global_Attribute9||CHR(9)||
				C1.Global_Attribute10||CHR(9)||
				C1.Global_Attribute11||CHR(9)||
				C1.Global_Attribute12||CHR(9)||
				C1.Global_Attribute13||CHR(9)||
				C1.Global_Attribute14||CHR(9)||
				C1.Global_Attribute15||CHR(9)||
				C1.Global_Attribute16||CHR(9)||
				C1.Global_Attribute17||CHR(9)||
				C1.Global_Attribute18||CHR(9)||
				C1.Global_Attribute19||CHR(9)||
				C1.Global_Attribute20||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				lv_SOB_SName||CHR(9)||
				CAPBREXT_COMMON.GetLocation(C1.Ship_To_Location_ID)||CHR(9)||
				CAPBREXT_COMMON.GetLocation(C1.Bill_To_Location_ID)||CHR(9)||
				CAPBREXT_COMMON.GetAPTerm(C1.Terms_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Accts_Pay_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Prepay_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Disc_Taken_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Res_Encumb_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetOrgName(C1.Inventory_Organization_ID)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Created_By)||CHR(9)||
				CAPBREXT_COMMON.GetBGName(C1.Business_Group_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Rate_Var_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Rate_Var_Gain_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Rate_Var_Loss_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetOrgName(C1.Org_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Future_Dated_Payment_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Expense_Clearing_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Misc_Charge_CCID)
			);

			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_SOB_SName||': No such set of books exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/